package com.gshrms.web.controller.system;

import com.gshrms.common.annotation.Log;
import com.gshrms.common.constant.CacheConstants;
import com.gshrms.common.core.controller.BaseController;
import com.gshrms.common.core.domain.AjaxResult2;
import com.gshrms.common.core.domain.entity.SysUser;
import com.gshrms.common.core.domain.model.RegisterBody;
import com.gshrms.common.enums.BusinessType;
import com.gshrms.common.utils.SecurityUtils;
import com.gshrms.common.utils.StringUtils;
import com.gshrms.framework.web.service.SysRegisterService;
import com.gshrms.system.mapper.SysUserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.gshrms.common.constant.Constants;
import com.gshrms.common.core.domain.AjaxResult;
import com.gshrms.common.core.domain.model.LoginBody;
import com.gshrms.framework.web.service.SysLoginService;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@RestController
@Api(tags = "登录/注册/忘记密码")
public class SysLoginController extends BaseController {

    @Autowired
    private SysLoginService loginService;
    @Autowired
    private SysRegisterService registerService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private SysUserMapper userMapper;


    @PostMapping("/register")
    @ApiOperation("应聘者注册")
    @Log(title = "应聘者注册", businessType = BusinessType.INSERT)
    public AjaxResult2 register(@RequestBody RegisterBody user) {
        boolean verifyCode = verifyCode(user.getUsername(), user.getVerificationCode());
        if (!verifyCode) {
            return AjaxResult2.error("短信验证码错误，请重试！");
        }
        String msg = registerService.register(user);
        if (StringUtils.isEmpty(msg)) {
            AjaxResult2 ajax = AjaxResult2.success();
            // 生成令牌
            String token = loginService.login(user.getUsername(), user.getPassword(), null, null);
            ajax.put(Constants.TOKEN, token);
            return ajax;
        }
        return AjaxResult2.error(msg);
    }

    /**
     * @param loginBody 登录信息
     * @return 结果
     */
    @ApiOperation("登录接口")
    @PostMapping("/login")
    @Log(title = "登录", businessType = BusinessType.INSERT)
    public AjaxResult2 login(@RequestBody LoginBody loginBody) {
        AjaxResult2 ajax = AjaxResult2.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(), loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    @PostMapping("/forgotPassword")
    @ApiOperation("忘记密码")
    @Log(title = "忘记密码", businessType = BusinessType.UPDATE)
    public AjaxResult forgotPassword(@RequestBody RegisterBody user) {
        boolean verifyCode = verifyCode(user.getUsername(), user.getVerificationCode());
        if (!verifyCode) {
            return error("短信验证码错误，请重试！");
        }
        SysUser sysUser = new SysUser();
        sysUser.setUserName(user.getUsername());
        sysUser.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        return toAjax(userMapper.updateUserPassword(sysUser));
    }

    // 验证验证码
    public boolean verifyCode(String phone, String inputCode) {
        String codeKey = String.format(CacheConstants.CODE_KEY, phone);
        String storedCode = redisTemplate.opsForValue().get(codeKey);
        return inputCode != null && inputCode.equals(storedCode);
    }
}
